package com.richfit.cuba.modular.util;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.richfit.cuba.modular.lsms.entity.SafeEncrypted;
import com.richfit.cuba.modular.lsms.entity.SafeReport;
import com.richfit.cuba.modular.lsms.entity.SafeReportData;
import com.richfit.cuba.modular.lsms.entity.SafeReportDetail;
import com.richfit.cuba.modular.lsms.entity.SafeTampering;
import com.richfit.cuba.modular.lsms.service.ISafeEncryptedService;
import com.richfit.cuba.modular.lsms.service.ISafeReportDataService;
import com.richfit.cuba.modular.lsms.service.ISafeReportDetailService;
import com.richfit.cuba.modular.lsms.service.ISafeReportService;
import com.richfit.cuba.modular.lsms.service.ISafeTamperingService;

import cn.hutool.core.collection.CollectionUtil;

@Component
public class CommonUtils {
	@Autowired
	private ISafeReportService safeReportService;
	@Autowired
	private ISafeReportDetailService safeReportDetailService;
	@Autowired
	private ISafeReportDataService safeReportDataService;
	
	@Autowired
	private ISafeTamperingService safeTamperingService;
	@Autowired
	private ISafeEncryptedService safeEncryptedService;
	
	
	public void insertNullData(String businessName,String bussinessTable) {
		SafeReport safeReport=new SafeReport();
		safeReport.setBusinessDate(DateUtils.truncate(Calendar.getInstance().getTime(), Calendar.DATE));
		safeReport.setBussinessTable(bussinessTable);
		safeReport.setTableDataTotal(0);
		safeReport.setCurrentScanDataCount(0);
		
		preInsert(safeReport);
		safeReport.setId(UUID.randomUUID().toString().replace("-", ""));
		safeReportService.save(safeReport);
	}
	
	/**
	 * 
	 * @Title: insertReportData   
	 * @Description: TODO 
	 * @author: Alan
	 * @date:   2019-03-03 17:58:30
	 * @param businessName  bussiness name in LSMS system
	 * @param bussinessTable  table name in LSMS database
	 * @param correctCount   validate correct data number
	 * @param totalCount  total data number
	 * @return  void      
	 */
	public void insertReportData(SafeReportDetail safeReportDetail) {
		safeReportDetail.setId(UUID.randomUUID().toString().replace("-", ""));
		safeReportDetailService.save(safeReportDetail);
		List<SafeReportData> dataList = safeReportDetail.getSafeReportDataList();
		if(CollectionUtil.isNotEmpty(dataList)) {
			for(SafeReportData data: dataList) {
				data.setReportDetailId(safeReportDetail.getId());
				data.setId(UUID.randomUUID().toString().replace("-", ""));
			}
			safeReportDataService.saveBatch(dataList);
		}
	}
	
	
	public SafeReport saveSaveReport(SafeReport safeReport) {
		// new data
		if(StringUtils.isBlank(safeReport.getId())) {
			safeReport.setId(UUID.randomUUID().toString().replace("-", ""));
			safeReportService.save(safeReport);
		} else {
			safeReportService.updateById(safeReport);
		}
		
		return safeReport;
	}
	
	public SafeReport getSafeReportByDate(Date date, String tableName) {
		if(date == null || StringUtils.isBlank(tableName)) {
			return null;
		}
		QueryWrapper <SafeReport> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("BUSSINESS_DATE", DateUtils.truncate(date, Calendar.DATE));
		queryWrapper.eq("BUSSINESS_TABLE", tableName);
		return safeReportService.getOne(queryWrapper);
	}
	/**
	 * Gets the string of the current certificate from the tamper resistant table
	 * @return String
	 */
	public  SafeTampering selectCert() {
//		String certificateString="";
		QueryWrapper<SafeTampering> entity=new QueryWrapper<SafeTampering>();
		entity.eq("DEL_FLAG", ScanStatus.TABLE_DEFLAGE_NO);
		entity.orderByAsc("DEL_FLAG");
		entity.orderByDesc("CREATE_DATE");
		IPage<SafeTampering> page = new Page<SafeTampering>(0,1);
		page = safeTamperingService.page(page, entity);
		
		SafeTampering safeTampering = null;
		if(page != null && CollectionUtil.isNotEmpty(page.getRecords())) {
			safeTampering=page.getRecords().get(0);
		}
		//SafeTampering safeTampering=safeTamperingService.getOne(entity);
//		if(safeTampering!=null) {
//			 certificateString=safeTampering.getPublicKey();	
//		}
		return safeTampering;
	}
	/**
	 * 
	 * Find encrypted table whether there is encrypted data recorded in this article.
	 */
	public SafeEncrypted selectCiphertext(String objId) {
		SafeEncrypted obj=safeEncryptedService.getById(objId);
		if(obj==null) {
			return null;
		}else {
			return obj;
		}
	}
	
	/**
	 * 
	 * @Title: preInsert   
	 * @Description: fill the filed update_date, create_date 
	 * @author: Alan
	 * @date:   2019-03-04 11:23:40
	 * @param safeReport      
	 * @return  void      
	 * @throws null
	 */
	private void preInsert(SafeReport safeReport) {
		Date time = Calendar.getInstance().getTime();
		safeReport.setCreateDate(time);
		safeReport.setUpdateDate(time);
	}
	
}
